<?php

/**
*
* @package ppkBB3cker
* @version $Id: cssjs.php 1.000 2010-12-18 18:52:00 PPK $
* @copyright (c) 2010 PPK
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

error_reporting(0);
@ini_set('register_globals', 0);
@ini_set('magic_quotes_runtime', 0);
@ini_set('magic_quotes_sybase', 0);

function_exists('date_default_timezone_set') ? date_default_timezone_set('Europe/Moscow') : '';

define('IN_PHPBB', true);

//From /includes/constants.php
@define('CHMOD_ALL', 7);
@define('CHMOD_READ', 4);
@define('CHMOD_WRITE', 2);
@define('CHMOD_EXECUTE', 1);

$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './../';
$phpEx = substr(strrchr(__FILE__, '.'), 1);

$type=@$_GET['type'];
if(!in_array($type, array('css', 'js')))
{
	exit();
}

$ppkbb3cker_addons="{$phpbb_root_path}tracker/addons/";
$addit['css']=array(
	$ppkbb3cker_addons.'css/spoiler.css',
	//$ppkbb3cker_addons.'css/easySlider.css',
	$ppkbb3cker_addons.'css/ppkbb3cker.css',
	$ppkbb3cker_addons.'css/prettyPhoto.css',
	$ppkbb3cker_addons.'css/jquery.tooltip.css',
);

$addit['js']=array(
	$ppkbb3cker_addons.'js/jquery.tooltip.js',
	$ppkbb3cker_addons.'js/jquery.prettyPhoto.js',
	$ppkbb3cker_addons.'js/spoiler.js',
	$ppkbb3cker_addons.'js/easySlider.js',
	$ppkbb3cker_addons.'js/ppkbb3cker.js',
);

echo get_tracker_cssjs($addit[$type], $type);

################################################################################
function get_tracker_cssjs($flist, $type)
{
	global $phpbb_root_path, $phpEx;

	if(is_array($flist) && sizeof($flist) && in_array($type, array('css', 'js')))
	{
		$md5_flist=md5(implode('|', $flist));
		$cssjs_file=$phpbb_root_path.'cache/data_ppkbb3cker_'.$type.'_'.$md5_flist.".{$phpEx}";
		if(@file_exists($cssjs_file))
		{
			$last_modified=@filemtime($cssjs_file);
			if($last_modified)
			{
				//$etag=dechex(@fileinode($cssjs_file)).'-'.dechex(@filesize($cssjs_file)).'-'.dechex($last_modified);
				$last_modified=gmdate('D, d M Y H:i:s', $last_modified).' GMT';
				if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']))
				{
					$if_modified_since=preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE']);
					if($if_modified_since==$last_modified)
					{
						header('HTTP/1.0 304 Not Modified');
						header('Cache-Control: max-age=86400, must-revalidate');
						exit;
					}
				}
				header('Last-Modified: '.$last_modified);
				header('Cache-Control: max-age=86400, must-revalidate');
			}
			header("Content-Type: ".($type=='css' ? "text/{$type}" : 'application/x-javascript')."; charset=UTF-8");
			$cssjs=@file($cssjs_file);
			if(sizeof($cssjs) > 2)
			{
				unset($cssjs[0]);
			}
			return implode('', $cssjs);
		}
		else
		{
			return write_tracker_cssjs($cssjs_file, $flist, $type);
		}
	}
}

function write_tracker_cssjs($cssjs_file, $flist, $type)
{
	global $phpbb_root_path;

	if($cssjs_file && is_writable($phpbb_root_path.'cache/'))
	{
		$cssjs_text='';
		foreach($flist as $fname)
		{
			if(@file_exists($fname))
			{
				$cssjs=file($fname);
				$cssjs ? $cssjs_text.=implode('', $cssjs)."\n" : '';
			}
		}

		$fo=@fopen($cssjs_file, 'wb');
		if($fo)
		{
			@flock($fo, LOCK_EX);
			@fwrite($fo, "<?php if (!defined('IN_PHPBB')) exit;?>\n{$cssjs_text}\n");
			//@fflush($fo);
			@flock($fo, LOCK_UN);
			@fclose($fo);

			phpbb_chmod($cssjs_file, CHMOD_READ | CHMOD_WRITE);
		}
		$last_modified=@filemtime($cssjs_file);
		if($last_modified)
		{
			//$etag=dechex(@fileinode($cssjs_file)).'-'.dechex(@filesize($cssjs_file)).'-'.dechex($last_modified);
			$last_modified=gmdate('D, d M Y H:i:s', $last_modified).' GMT';
			if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']))
			{
				$if_modified_since=preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE']);
				if($if_modified_since==$last_modified)
				{
					header('HTTP/1.0 304 Not Modified');
					header('Cache-Control: max-age=86400, must-revalidate');
					exit;
				}
			}
			header('Last-Modified: '.$last_modified);
			header('Cache-Control: max-age=86400, must-revalidate');
		}
		echo header("Content-Type: ".($type=='css' ? "text/{$type}" : 'application/x-javascript')."; charset=UTF-8");
		return $cssjs_text;
	}
}

//From /includes/functions.php
function phpbb_chmod($filename, $perms = CHMOD_READ)
{
	static $_chmod_info;

	// Return if the file no longer exists.
	if (!file_exists($filename))
	{
		return false;
	}

	// Determine some common vars
	if (empty($_chmod_info))
	{
		if (!function_exists('fileowner') || !function_exists('filegroup'))
		{
			// No need to further determine owner/group - it is unknown
			$_chmod_info['process'] = false;
		}
		else
		{
			global $phpbb_root_path, $phpEx;

			// Determine owner/group of common.php file and the filename we want to change here
			$common_php_owner = @fileowner($phpbb_root_path . 'common.' . $phpEx);
			$common_php_group = @filegroup($phpbb_root_path . 'common.' . $phpEx);

			// And the owner and the groups PHP is running under.
			$php_uid = (function_exists('posix_getuid')) ? @posix_getuid() : false;
			$php_gids = (function_exists('posix_getgroups')) ? @posix_getgroups() : false;

			// If we are unable to get owner/group, then do not try to set them by guessing
			if (!$php_uid || empty($php_gids) || !$common_php_owner || !$common_php_group)
			{
				$_chmod_info['process'] = false;
			}
			else
			{
				$_chmod_info = array(
					'process'		=> true,
					'common_owner'	=> $common_php_owner,
					'common_group'	=> $common_php_group,
					'php_uid'		=> $php_uid,
					'php_gids'		=> $php_gids,
				);
			}
		}
	}

	if ($_chmod_info['process'])
	{
		$file_uid = @fileowner($filename);
		$file_gid = @filegroup($filename);

		// Change owner
		if (@chown($filename, $_chmod_info['common_owner']))
		{
			clearstatcache();
			$file_uid = @fileowner($filename);
		}

		// Change group
		if (@chgrp($filename, $_chmod_info['common_group']))
		{
			clearstatcache();
			$file_gid = @filegroup($filename);
		}

		// If the file_uid/gid now match the one from common.php we can process further, else we are not able to change something
		if ($file_uid != $_chmod_info['common_owner'] || $file_gid != $_chmod_info['common_group'])
		{
			$_chmod_info['process'] = false;
		}
	}

	// Still able to process?
	if ($_chmod_info['process'])
	{
		if ($file_uid == $_chmod_info['php_uid'])
		{
			$php = 'owner';
		}
		else if (in_array($file_gid, $_chmod_info['php_gids']))
		{
			$php = 'group';
		}
		else
		{
			// Since we are setting the everyone bit anyway, no need to do expensive operations
			$_chmod_info['process'] = false;
		}
	}

	// We are not able to determine or change something
	if (!$_chmod_info['process'])
	{
		$php = 'other';
	}

	// Owner always has read/write permission
	$owner = CHMOD_READ | CHMOD_WRITE;
	if (is_dir($filename))
	{
		$owner |= CHMOD_EXECUTE;

		// Only add execute bit to the permission if the dir needs to be readable
		if ($perms & CHMOD_READ)
		{
			$perms |= CHMOD_EXECUTE;
		}
	}

	switch ($php)
	{
		case 'owner':
			$result = @chmod($filename, ($owner << 6) + (0 << 3) + (0 << 0));

			clearstatcache();

			if (is_readable($filename) && phpbb_is_writable($filename))
			{
				break;
			}

		case 'group':
			$result = @chmod($filename, ($owner << 6) + ($perms << 3) + (0 << 0));

			clearstatcache();

			if ((!($perms & CHMOD_READ) || is_readable($filename)) && (!($perms & CHMOD_WRITE) || phpbb_is_writable($filename)))
			{
				break;
			}

		case 'other':
			$result = @chmod($filename, ($owner << 6) + ($perms << 3) + ($perms << 0));

			clearstatcache();

			if ((!($perms & CHMOD_READ) || is_readable($filename)) && (!($perms & CHMOD_WRITE) || phpbb_is_writable($filename)))
			{
				break;
			}

		default:
			return false;
		break;
	}

	return $result;
}

function phpbb_is_writable($file)
{
	if (strtolower(substr(PHP_OS, 0, 3)) === 'win' || !function_exists('is_writable'))
	{
		if (file_exists($file))
		{
			// Canonicalise path to absolute path
			$file = phpbb_realpath($file);

			if (is_dir($file))
			{
				// Test directory by creating a file inside the directory
				$result = @tempnam($file, 'i_w');

				if (is_string($result) && file_exists($result))
				{
					unlink($result);

					// Ensure the file is actually in the directory (returned realpathed)
					return (strpos($result, $file) === 0) ? true : false;
				}
			}
			else
			{
				$handle = @fopen($file, 'r+');

				if (is_resource($handle))
				{
					fclose($handle);
					return true;
				}
			}
		}
		else
		{
			// file does not exist test if we can write to the directory
			$dir = dirname($file);

			if (file_exists($dir) && is_dir($dir) && phpbb_is_writable($dir))
			{
				return true;
			}
		}

		return false;
	}
	else
	{
		return is_writable($file);
	}
}
?>
